package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.PurchaseListDao;
import com.atguigu.jxc.dao.ReturnListDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.atguigu.jxc.service.ReturnListGoodsService;
import com.atguigu.jxc.service.UserService;
import com.atguigu.jxc.to.PurchaseTo;
import com.atguigu.jxc.to.ReturnCountTo;
import com.atguigu.jxc.to.ReturnTo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:PurchaseListGoodsServiceImpl
 * @Version: 1.0
 * @CreateAt:2021/3/8 10:30
 * @Author: ✨HanSan·胡✨
 * @Description:
 */
@Service
public class ReturnListGoodsServiceImpl implements ReturnListGoodsService {
    @Autowired
    ReturnListDao returnListDao;


    @Transactional
    @Override
    public ServiceVO save(ReturnList returnList, String returnListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        returnList.setUserId(user.getUserId());
        returnListDao.save(returnList);
        Gson gson = new Gson();
        List<ReturnListGoods> list = gson.fromJson(returnListGoodsStr,new TypeToken<List<ReturnListGoods>>(){}.getType());
        for (ReturnListGoods returnListGoods : list) {
            returnListGoods.setReturnListId(returnList.getReturnListId());
        }
        if (list.size()>0){
            returnListDao.saveReturnListGoods(list);
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map listByPage(ReturnTo returnTo) {
        if(null == returnTo.getPageSize() ||returnTo.getPageSize()<=0 ){
            returnTo.setPageSize(0);
        }else {
            returnTo.setPageSize(returnTo.getPageSize()-1);
        }

        if(null == returnTo.getPageCount() ||returnTo.getPageCount()<20 ){
            returnTo.setPageCount(20);
        }
        Map map = new HashMap();
        List<Map> list = returnListDao.listByPage(returnTo);
        map.put("rows",list);
        map.put("page_now",returnTo.getPageSize());
        map.put("size_now",returnTo.getPageCount());
        return map;
    }

    @Override
    public Map goodsList(Integer returnListId) {
        Map map = new HashMap();
        List<ReturnListGoods> returnListGoods = returnListDao.goodsList(returnListId);
        map.put("rows",returnListGoods);
        return map;
    }

    @Transactional
    @Override
    public ServiceVO delete(Integer returnListId) {
        returnListDao.deleteByReturnListGoods(returnListId);
        returnListDao.deleteByReturnList(returnListId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO updateState(Integer returnListId) {
        returnListDao.updateState(returnListId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<Map> count(ReturnCountTo returnCountTo) {
        String codeOrName = returnCountTo.getCodeOrName();
        if(null!=codeOrName &&  !"".equals(codeOrName)){
            try {
                int i = Integer.parseInt(codeOrName);
                returnCountTo.setStatus(1);
            }catch (Exception e){
                returnCountTo.setStatus(2);
            }
        }

        List<Map> list = returnListDao.count(returnCountTo);
        return list;
    }

}
